package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.common.enums.RecordType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.XxMember;
import com.ruoyi.system.domain.request.XxMemberAmountRecordRequest;
import com.ruoyi.system.domain.vo.XxMemberAmountRecordVo;
import com.ruoyi.system.mapper.XxMemberMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.XxMemberAmountRecordMapper;
import com.ruoyi.system.domain.XxMemberAmountRecord;
import com.ruoyi.system.service.IXxMemberAmountRecordService;

/**
 * 会员金额记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-02-08
 */
@Service
public class XxMemberAmountRecordServiceImpl implements IXxMemberAmountRecordService 
{
    @Autowired
    private XxMemberAmountRecordMapper xxMemberAmountRecordMapper;

    @Autowired
    private XxMemberMapper xxMemberMapper;

    /**
     * 查询会员金额记录
     * 
     * @param recordId 会员金额记录主键
     * @return 会员金额记录
     */
    @Override
    public XxMemberAmountRecord selectXxMemberAmountRecordByRecordId(Long recordId)
    {
        return xxMemberAmountRecordMapper.selectXxMemberAmountRecordByRecordId(recordId);
    }

    /**
     * 查询会员金额记录列表
     * 
     * @param request 查询结果
     * @return 会员金额记录
     */
    @Override
    public List<XxMemberAmountRecordVo> selectXxMemberAmountRecordList(XxMemberAmountRecordRequest request)
    {
        return xxMemberAmountRecordMapper.selectXxMemberAmountRecordList(request);
    }

    /**
     * 新增会员金额记录
     * 
     * @param xxMemberAmountRecord 会员金额记录
     * @return 结果
     */
    @Override
    public int insertXxMemberAmountRecord(XxMemberAmountRecord xxMemberAmountRecord)
    {
        xxMemberAmountRecord.setCreateTime(DateUtils.getNowDate());

        //
        XxMember xxMember = xxMemberMapper.selectXxMemberByMemberId(xxMemberAmountRecord.getMemberId());
        if (xxMember == null) {
            throw new ServiceException("会员不存在");
        }

        BigDecimal recordBeforeAmount = xxMember.getMemberBalanceAmount();
        BigDecimal recordBeforeGiftAmount = xxMember.getMemberGiftBalanceAmount();

        if (RecordType.RECHARGE.getCode().equals(xxMemberAmountRecord.getRecordType())) {
            // 充值
            xxMember.setMemberGiftBalanceAmount(xxMember.getMemberGiftBalanceAmount().add(xxMemberAmountRecord.getRecordAmount()));
            xxMember.setMemberBalanceAmount(xxMember.getMemberBalanceAmount().add(xxMemberAmountRecord.getRecordAmount()).add(xxMember.getMemberGiftBalanceAmount()));
            xxMemberMapper.updateXxMember(xxMember);
        } else if (RecordType.CONSUME.getCode().equals(xxMemberAmountRecord.getRecordType())) {
            // 消费
            if(xxMember.getMemberGiftBalanceAmount().compareTo(xxMemberAmountRecord.getRecordAmount()) < 0) {
                xxMember.setMemberGiftBalanceAmount(BigDecimal.ZERO);
            }else {
                xxMember.setMemberGiftBalanceAmount(xxMember.getMemberGiftBalanceAmount().subtract(xxMemberAmountRecord.getRecordAmount()));
            }

            xxMember.setMemberBalanceAmount(xxMember.getMemberBalanceAmount().subtract(xxMemberAmountRecord.getRecordAmount()));

            xxMemberMapper.updateXxMember(xxMember);
        }

        BigDecimal recordAfterAmount = xxMember.getMemberBalanceAmount();
        BigDecimal recordAfterGiftAmount = xxMember.getMemberGiftBalanceAmount();
        xxMemberAmountRecord.setRecordBeforeAmount(recordBeforeAmount);
        xxMemberAmountRecord.setRecordAfterAmount(recordAfterAmount);
        xxMemberAmountRecord.setRecordBeforeGiftAmount(recordBeforeGiftAmount);
        xxMemberAmountRecord.setRecordAfterGiftAmount(recordAfterGiftAmount);
        xxMemberAmountRecord.setRecordGiftAmount(recordBeforeGiftAmount.subtract(recordAfterGiftAmount));

        return xxMemberAmountRecordMapper.insertXxMemberAmountRecord(xxMemberAmountRecord);
    }

    /**
     * 修改会员金额记录
     * 
     * @param xxMemberAmountRecord 会员金额记录
     * @return 结果
     */
    @Override
    public int updateXxMemberAmountRecord(XxMemberAmountRecord xxMemberAmountRecord)
    {
        xxMemberAmountRecord.setUpdateTime(DateUtils.getNowDate());
        return xxMemberAmountRecordMapper.updateXxMemberAmountRecord(xxMemberAmountRecord);
    }

    /**
     * 批量删除会员金额记录
     * 
     * @param recordIds 需要删除的会员金额记录主键
     * @return 结果
     */
    @Override
    public int deleteXxMemberAmountRecordByRecordIds(Long[] recordIds)
    {
        return xxMemberAmountRecordMapper.deleteXxMemberAmountRecordByRecordIds(recordIds);
    }

    /**
     * 删除会员金额记录信息
     * 
     * @param recordId 会员金额记录主键
     * @return 结果
     */
    @Override
    public int deleteXxMemberAmountRecordByRecordId(Long recordId)
    {
        return xxMemberAmountRecordMapper.deleteXxMemberAmountRecordByRecordId(recordId);
    }
}
